package com.ruoyi.odorservice.model.bean;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class ClusterBean {
    @ApiModelProperty("质心")
    double[] centroid;
    @ApiModelProperty("所含点位")
    List<double[]> points;

    public ClusterBean(double[] centroid) {
        this.centroid = centroid;
        this.points = new ArrayList<>();
    }

    public void addPoint(double[] p) {
        points.add(p);
    }

    public void calculateCentroid() {
        double[] sum = new double[centroid.length];
        for (double[] p : points) {
            for (int i = 0; i < centroid.length; i++) {
                sum[i] += p[i];
            }
        }
        for (int i = 0; i < centroid.length; i++) {
            centroid[i] = sum[i] / points.size();
        }
    }
}
